<template>
    <div class="check-overdue">
        <div class="table">
            <el-table :data="tableData" border fit style="width: 100%" header-cell-class-name="table-header-cell"
            :class="tableData.length === 0 ? 'table-empty' : ''" v-loading="loading" @selection-change="handleSelectionChange">
              <el-table-column type="selection" width="55" align="center" />
              <el-table-column label="行政代码" align="center" prop="code" />
              <el-table-column label="村名" align="center" prop="name" />
              <el-table-column label="镇行政" align="center" prop="streetName" />
              <el-table-column label="省行政" align="center" prop="provinceName" />
              <el-table-column label="市行政" align="center" prop="cityName" />
              <el-table-column label="区行政" align="center" prop="areaName" />
              <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
                <template slot-scope="scope">
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-edit"
                    @click="handleUpdate(scope.row)"
                    v-hasPermi="['his:village:edit']"
                  >修改</el-button>
                  <el-button
                    size="mini"
                    type="text"
                    icon="el-icon-delete"
                    @click="handleDelete(scope.row)"
                    v-hasPermi="['his:village:remove']"
                  >删除</el-button>
                </template>
              </el-table-column>
                <!--<el-table-column label="查看" width="300">
                    <template slot-scope="scope">
                        <div class="operation">
                            <div  style="margin-left: 20%;">添加/修改</div>
                        </div>
                    </template>
                </el-table-column>-->
            </el-table>
        </div>
      <pagination
        v-show="total>0"
        :total="total"
        :page.sync="queryParams.pageNum"
        :limit.sync="queryParams.pageSize"
        @pagination="getList"
      />
      <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body>
        <el-form ref="form" :model="form" :rules="rules" label-width="80px">
          <el-form-item label="行政代码" prop="code">
            <el-input v-model="form.code" placeholder="请输入行政代码" />
          </el-form-item>
          <el-form-item label="村名" prop="name">
            <el-input v-model="form.name" placeholder="请输入村名" />
          </el-form-item>
        </el-form>
        <div slot="footer" class="dialog-footer">
          <el-button type="primary" @click="submitForm">确 定</el-button>
          <el-button @click="cancel">取 消</el-button>
        </div>
      </el-dialog>
    </div>
</template>
<script>
  import { listVillage, getVillage, delVillage, addVillage, updateVillage, exportVillage } from "@/api/his/village";
export default {
    name: 'check-overdue',
    data() {
        return {
          // 遮罩层
          loading: true,
          // 导出遮罩层
          exportLoading: false,
          // 选中数组
          ids: [],
          // 非单个禁用
          single: true,
          // 非多个禁用
          multiple: true,
          // 显示搜索条件
          showSearch: true,
          // 总条数
          total: 0,
          // 村表格数据
          villageList: [],
          // 弹出层标题
          title: "",
          // 是否显示弹出层
          open: false,
          // 查询参数
          queryParams: {
            pageNum: 1,
            pageSize: 10,
            code: null,
            name: null,
            streetCode: null,
            provinceCode: null,
            cityCode: null,
            areaCode: null
          },
          // 表单参数
          form: {},
          // 表单校验
          rules: {
            code: [
              { required: true, message: "行政代码不能为空", trigger: "blur" }
            ],
          },
            tableData: [

            ]
        }
    },
  created() {
    this.getList();
  },
  methods: {
    /** 查询村列表 */
    getList() {
      this.loading = true;
      listVillage(this.queryParams).then(response => {
        this.tableData = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        code: null,
        name: null,
        streetCode: null,
        provinceCode: null,
        cityCode: null,
        areaCode: null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.code)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加村";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      const code = row.code || this.ids
      getVillage(code).then(response => {
        this.form = response.data;
        this.open = true;
        this.title = "修改村";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.code != null) {
            updateVillage(this.form).then(response => {
              this.$modal.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addVillage(this.form).then(response => {
              this.$modal.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const codes = row.code || this.ids;
      this.$modal.confirm('是否确认删除村编号为"' + codes + '"的数据项？').then(function() {
        return delVillage(codes);
      }).then(() => {
        this.getList();
        this.$modal.msgSuccess("删除成功");
      }).catch(() => {});
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$modal.confirm('是否确认导出所有村数据项？').then(() => {
        this.exportLoading = true;
        return exportVillage(queryParams);
      }).then(response => {
        this.$download.name(response.msg);
        this.exportLoading = false;
      }).catch(() => {});
    }
  }
}
</script>
<style scoped lang="scss">
.check-overdue {
    height: 100%;
    display: flex;
    flex-direction: column;
}
.table {
    flex: 1;
    background-color: #fff;
    margin: 13px 0 16px;

    ::v-deep .table-header-cell {
        background-color: #EBEFF8;
        padding-left: 21px;
        font-weight: 500;
        font-size: 16px;
        color: #414141;
    }

    .el-table {
        height: 100%;
    }

    ::v-deep .el-table__cell {
        padding-left: 21px;
        font-weight: 500;
        font-size: 16px;
        color: #414141;
    }

    :v-deep .el-table__body-wrapper {
        height: 100%;
    }

    :v-deep .el-table__empty-block {
        height: 100%;
    }

    .operation {
        display: flex;
        align-items: center;
        height: 100%;

        .edit {
            cursor: pointer;
            font-weight: 500;
            font-size: 16px;
            color: #177AD0;
            height: 16px;
            line-height: 16px;
            background-repeat: no-repeat;
            background-position: 0 center;
            margin-right: 20px;
            user-select: none;
        }
    }
}
</style>
